coos.extend(String.prototype,{
	trim : function () 
	{
	    return this.replace(/(^\s*)|(\s*$)/g, "");
	},
	ltrim : function () 
	{
	    return this.replace(/^\s*/g, "");
	},
	rtrim : function ()
	{
	    return this.replace(/\s*$/g, "");
	},
	Trim : function() 
	{ 
	    return this.replace(/(^\s*)|(\s*$)/g, ""); 
	},
	LTrim : function() 
	{ 
	    return this.replace(/(^\s*)/g, ""); 
	},
	Rtrim : function() 
	{ 
	    return this.replace(/(\s*$)/g, ""); 
	},
	contains : function (string, s) 
	{
	    return (s) ? (s + this + s).indexOf(s + string + s) > -1 : this.indexOf(string) > -1;
	},
	equals : function () 
	{
	    for (var i = 0; i < arguments.length; i++) 
		{
	        if (this == arguments[i])
	            return true;
	    }
	    return false;
	},
	startsWith : function (A) 
	{
	    return (this.substr(0, A.length) == A);
	},
	endsWith : function (A, B) 
	{
	    var C = this.length;
	    var D = A.length;
	    if (D > C) 
		{
	        return false;
	    }
	    if (B) 
		{
	        var E = new RegExp(A + "$", "i");
	        return E.test(this);
	    } 
		else
		{
	        return (D == 0 || this.substr(C - D, D) == A);
	    }
	},
	remove : function (A, B)
	{
	    var s = "";
	    if (A > 0) {
	        s = this.substring(0, A);
	    }
	    if (A + B < this.length)
		{
	        s += this.substring(A + B, this.length);
	    }
	    return s;
	},
	replaceNewLineChars : function (A) 
	{
	    return this.replace(/\n/g, A);
	},
	escape : function () 
	{
		return this.replace(/('|\\)/g, "\\$1");
	},
	/**
	 * 字符串截取
	 */
	intercept : function(len)
	{
		if(this.length*2 <= len)
			return this;
		var strlen = 0; 
		var s = "";
		for(var i = 0; i < this.length; i++)
		{
			if(this.charCodeAt(i) > 128)
			{			
				strlen = strlen + 2;
				if(strlen > len)
					return s.substring(0,s.length-1) + "...";
			}
			else
			{
				strlen = strlen + 1;
				if(strlen > len)
					return s.substring(0,s.length-2) + "...";
			}
			s = s + this.charAt(i);
		}
		return s;
	},
	/**
	 * 字符串加法
	 * abc.times(3)=abcabcabc
	 */
	times : function(n)
	{
		var s = this;
		var total = "";
		while(n >0)
		{
			if(n%2 == 1)
			{
				total += s;
			}
			if(n == 1)
			{
				break;
			}
			s += s;
			n = n >> 1;
		}
		return total;
	},
	/**
	 * 批量替换字符
	 * var str = "<div>d&dkkd</div>";
	 * str = str.multiReplace({'&(?!#?\\w+;)' :'&amp;','"([^"]*)" : '“$1”','<' : '&lt;' ,'>' : '&gt;'})
	 */
	multiReplace : function(hash)
	{
		var str = this;
		for(var key in hash)
		{
			if(Object.prototype.hasOwnProperty.call(hash,key))
			{
				str = str.rplace(new RegExp(key,"g"),hash[key]);
			}
		}
		return str;
	},
	getFileType : function()
	{
		return this.substring(this.lastIndexOf(".")+1,this.length).toLowerCase();
	},
	toInt : function()
	{
		return parseInt(this, 10);
	},
	toFloat : function()
	{
		return parseFloat(this);
	},
	toArray : function(sp)
	{
		if(sp)
		{
			return this.trim().split(sp);
		}
		var arr = new Array();
		for(var i = 0; i < this.length; i++)
		{
			arr[i] = this.substr(i,1);
		}
		return arr;
	},
	random : function()
	{
		var arr = this.toArray();
		return arr[Math.floor(Math.random() * arr.length)] || null;
	},
	clean : function()
	{
		return this.replace(/\s{2,}/g, ' ').trim();
	},
	toJson : function()
	{
		if(this==""){
			return eval('({})');
		}
		return eval('(' + this + ')');
	}
});


function StringBuffer()
{
	var arr = new Array;
	return{
		append : function(str)
		{
			arr[arr.length] = str;  
		},
		toString : function()
		{
			return arr.join(""); 
		}
	}
}
coos.extend(Array.prototype,{
	clear : function () 
	{
		this.length = 0;
		return this;
	},
	first : function () 
	{
		return this[0];
	},
	last : function () 
	{
		return this[this.length - 1];
	},
	size : function () 
	{
		return this.length;
	},
	contains : function (value) 
	{
		for(var i in this)
		{
			if(this[i] == value)  return true;
		}
		return false;
	},
	append : function(value) 
	{
		this.push(value);
		return this;
	},
	remove: function(value) 
	{
		for(var i in this)
		{
			if(this[i] == value)
			{
				this.splice(i,1);
			}
		}
		return this;
	},
	random : function(min, max)
	{
		if(min && max)
		{
			return this[Math.floor(Math.random() * (max - min + 1) + min)] || null;
		}
		else
		{
			return this[Math.floor(Math.random() * this.length)] || null;
		}
	},
	copy : function(value) 
	{
	    var temp = [];
		for(var i in this)
		{
			temp[i] = this[i];
		}
		return temp;
	},
	compare : function(arr)
	{
		for(var i in arr)
		{
			if(!this.contains(arr[i])) return false;
		}
		for(var j in this)
		{
			if(!arr.contains(this[j])) return false;
		}
		return true;
	},
	indexOf : function(item, from)
	{
		var len = this.length;
		for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++)
		{
			if (this[i] === item) 
			{
				return i;
			}
		}
		return -1;
	},
	lastIndexOf : function (item, from) 
	{
		var len = this.length;
		for (var i = (from < len) ? Math.max(0, len - from) : len || 0; i > -1; i--)
		{
			if (this[i] === item) 
			{
				return i;
			}
		}
		return -1;
	},
	count : function () 
	{
		var count = this.length;
		var len = this.length;
		for(var i = 0; i < len; i++)
		{
			for(var j = i+1; j < len; j++)
			{
				if(this[i] == this[j]) count--;
			}
		}
		return count;
	},
	isSingle : function(arr)
	{
		var len = arr.length;
		for(var i = 0; i < len; i++)
		{
			for(var j = i+1; j < len; j++)
			{
				if(arr[i] == arr[j]) return false;
			}
		}
		return true;
	},
	/**
	 * 获得不重复的值
	 */
	getSingle : function()
	{
		var results = [];
		for(var v in this)
		{
			if(!results.contains(this[v])){
				results.append(this[v]);
			}
		}
		return results;
	},
	sum : function() 
	{
	    var temp = 0;
		for(var i in this)
		{
			if(!isNaN(this[i]))
				temp += this[i];
		}
		return temp;
	},
	include : function(arr)
	{
		for(var i in arr)
		{
			if(!this.contains(arr[i])) return false;
		}
		return true;
	},
	innercount : function(arr)
	{
		var count = 0;
		for(var i in arr)
		{
			if(this.contains(arr[i])) count++;
		}
		return count;
	},
	outcount : function(arr)
	{
		var count = 0;
		for(var i in arr)
		{
			if(arr.contains(this[i])) count++;
		}
		return count;
	},
	/**
	 * function printElt(element, index, array) {
	    print("[" + index + "] is " + element); // assumes print is already defined
	}
	[2, 5, 9].forEach(printElt);
	// Prints:
	// [0] is 2
	// [1] is 5
	// [2] is 9
	 */
	forEach : function(fn, bind)
	{
		for (var i = 0, j = this.length; i < j; i++) fn.call(bind, this[i], i, this);
	},
	each : function(iterator) 
	{
	    for (var i = 0, length = this.length; i < length; i++)
	    {
	    	if(typeof this[i] != "function")
	      		iterator(this[i]);
	      	else
	      		this[i].call(this);
	    }
	},
	every : function(fn, bind)
	{
		for (var i = 0, j = this.length; i < j; i++){
			if (!fn.call(bind, this[i], i, this)) return false;
		}
		return true;
	},
	some : function(fn, bind)
	{
		for (var i = 0, j = this.length; i < j; i++){
			if (fn.call(bind, this[i], i, this)) return true;
		}
		return false;
	},
	map : function(fn, bind)
	{
		var results = [];
		for (var i = 0, j = this.length; i < j; i++) results[i] = fn.call(bind, this[i], i, this);
		return results;
	},
	filter : function(fn, bind)
	{
		var results = [];
		for (var i = 0, j = this.length; i < j; i++){
			if (fn.call(bind, this[i], i, this)) results.push(this[i]);
		}
		return results;
	},
	toArray : function(index)
	{
		var results = [];
		for (var i = index, j = this.length; i < j; i++){
			results.push(this[i]);
		}
		return results; 
	},
	toString : function(sp)
	{
		var temp = sp || "";
		return this.join(temp); 
	},
	end : {}
	
});
Number.prototype.NaN0 = function()
{
	return isNaN(this)?0:this;
};
/**
 * 
 *@
<pre>
 	var user = coos.hashmap();
	user.set("name","zhangsan");
	user.set("age","28");
	user.set("job","leader");
	var result1 = user.getValues().toString();

	var user2 = coos.hashmap();
	user2.set("username","zhangsan");
	user2.set("username","zhangsan");
	var result2 = user2.get("username");
	
	alert(result1 + "========" + result2);
</pre>
 */
coos.hashmap = function()
{
	function hash(_key, _value)
	{
		this.key	= _key;
		this.value	= _value;
	};
	var map = new Array();
	
	return {
		set : function(key,value)
		{
			for (var i = 0; i < map.length; i++)
			{
				if (map[i].key == key )
				{
					map[i].value = value;
					return;
				}
			}
			map[map.length] = new hash(key,value);			
		},
		get : function(key)
		{
			for (var i = 0; i < map.length; i++)
			{
				if (map[i].key == key)
					return map[i].value;
			}
			return null;
		},
		size : function()
		{
			return map.length;
		},
		getKey : function(index)
		{
			if (map.length <= index)
				return null;
			return map[index].key;
		},
		getValue : function(index)
		{
			if (map.length <= index)
				return null;
			return map[index].value;
		},
		getLastKey : function()
		{
			return map[map.length-1].key;
		},
		getLastValue : function()
		{
			return map[map.length-1].value;
		},
		getKeys : function()
		{
			var keys = [];
			map.forEach(function(_hash){
				keys.push(_hash.key);
			});
			return keys;
		},
		getValues : function()
		{
			var values = [];
			map.forEach(function(_hash){
				values.push(_hash.value);
			});
			return values;
		},
		containsKey : function(key)
		{
			return this.getKeys().contains(key);
		},
		containsValue : function(value)
		{
			return this.getValues().contains(value);
		},
		each : function(callback)
		{
			map.forEach(function(_hash){
				callback(_hash.key,_hash.value);
			});
		},
		toString : function()
		{
			var arr = [];
			map.forEach(function(_hash){
				arr.push("\"" + _hash.key + "\":\"" + _hash.value + "\"");
			});
			return "{" + arr.toString(",") + "}";
		},
		toJson : function()
		{
			return this.toString().toJson();
		},
		remove : function(key)
		{
			for(var i = 0; i < map.length; i++)
			{
				if(map[i].key == key)
					map.splice(i,1);
			}
		},
		isEmpty : function()
		{
			return (map.length == 0);
		},
		sort : function()
		{
			map = map.sort();
		},
		clear : function()
		{
			map = [];
		}
	};
};
coos.event = function(e)
{
	var e = e || window.event;
	return e;
};

coos.event.format = function(e) 
{
	var e = e || window.event;
	try
	{
		if(!e.pageX && e.clientX)//firefox3 nonsupport pageX
		{
			e.pageX = e.clientX + document.body.scrollLeft;
			e.pageY = e.clientY + document.documentElement.scrollTop;
		}
	}
	catch(e){}
	
	if(window.event)
	{
		e.charCode = (e.type == "keypress") ? e.keyCode : 0;
		e.eventPhase = 2;
		e.isChar = (e.charCode > 0);
		e.preventDefault = function ()
		{
			this.returnValue = false;
		};
		
		if(e.type == "mouseout")
		{
			e.relatedTarget = e.toElement;
		}
		else if(e.type == "mouseover")
		{
			e.relatedTarget = e.formElement;
		}
		
		e.stopPropagation = function ()
		{
			this.cancelBubble = true;
		};
		
		e.target = e.srcElement;
		e.time   = (new Date()).getTime();
	}
	
	try
	{
		if (e.target.nodeType == 3)
		{// defeat Safari bug
			e.target = e.target.parentNode;
		}
		//如果不存在keyCode属性，同时存在which属性则赋值,因为该属性为只读属性(has only a getter)
		if(!e.keyCode && e.which)
		{
			e.keyCode = e.which;
		}
	}
	catch(e){}
	
	return e;
};

coos.event.add = function(el,EventType,callback)
{
	var obj = coos.$obj(el);
	
	if(obj.addEventListener)
	{
		obj.addEventListener(EventType,callback,false);
	}
	else if(obj.attachEvent)
	{
		obj.attachEvent('on'+EventType,callback);
	} 
	else
	{
		obj['on'+EventType] = callback;
	}
};

coos.event.remove = function(el,EventType,callback)
{
	var obj = coos.$obj(el);
	
	if(obj.removeEventListener)
	{
		obj.removeEventListener(EventType,callback,false);
	} 
	else if(obj.detachEvent)
	{
		obj.detachEvent('on'+EventType,callback);
	} 
	else
	{
		obj['on'+EventType] = callback;
	}
};

coos.event.stop = function(e) 
{
    coos.event.format(e).stopPropagation();
};

coos.event.cancel = function(e) 
{
    coos.event.format(e).preventDefault();
};

coos.event.pageX = function(e)
{
	return coos.event.format(e).pageX;
};

coos.event.pageY = function(e)
{
	return coos.event.format(e).pageY;
};

coos.event.element = function(e)
{
	return coos.event.format(e).target;
};

coos.event.keyCode = function(e)
{
	return coos.event.format(e).keyCode;	
};
/**
 * 正则表达式的相关函数，在此集中管理，方便升级维护
 */
coos.regex = {
	//由于小写double int是javascript关键字，检测器会报错，所以采有大写的,表达式里面用\的必须用双斜杠\\代替,/用\/转义
	Double   : "^[0-9|-]+[\\.]?[0-9]*$",
	Int      : "^[0-9]+[0-9]*$",
	Date     : "^(\\d{1,4})(-|\/)(\\d{1,2})\\2(\\d{1,2})$",
	DateTime : "^(\\d{1,4})(-|\/)(\\d{1,2})\\2(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$",
	Letter   : "^[a-zA-Z]+$",//英文字母
	Chinese  : "^[\\u0391-\\uFFE5]+$",//中文
	Email    : "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$",
	Phone    : "^((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?$",    
	Mobile   : "1(3|5|8)[0-9]{9}$",//手机号码 13xx 15xx 18xx 开头
	Url      : "^[http|https]:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$",   
	IdCard   : "^\\d{15}(\\d{2}[A-Za-z0-9])?$", //身份证号码  
	QQ       : "^[1-9]\\d{4,9}$",
	ZIP      : "^\\d{6}$",//邮编
	Account  : "^\\w+$",//^[A-Za-z0-9_]+$ 英文字母或数字或下划线
	Empty    : ""
};

/**
 * 通用的test方法
 */
coos.regex.test = function(str,regex)
{
	var oReg = new RegExp(regex);
	return oReg.test(str);	
};

/**
 * 判断传入的字符串是否为数字
 */
coos.regex.isDouble = function(str)
{
	return coos.regex.test(str,coos.regex.Double);
};

/**
 * 判断传入的字符串是否为整数
 */
coos.regex.isInt = function(str)
{
	return coos.regex.test(str,coos.regex.Int);
};

/**
 *  判断传入的字符串是否为空
 */
coos.regex.isEmpty = function(str)
{
	return coos.regex.test(str,coos.regex.Empty);
};

/**
 *  判断传入的字符串是否为手机号码
 */
coos.regex.isMobile = function(str)
{
	return coos.regex.test(str,coos.regex.Mobile);
};

/**
 *  判断传入的字符串是否为帐号
 */
coos.regex.isAccount = function(str)
{
	return coos.regex.test(str,coos.regex.Account);
};

/**
 *  判断传入的字符串是否为QQ号码
 */
coos.regex.isQQ = function(str)
{
	return coos.regex.test(str,coos.regex.QQ);
};

/**
 *  判断传入的字符串是否为邮箱
 */
coos.regex.isEmail = function(str)
{
	return coos.regex.test(str,coos.regex.Email);
};
